/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.JConexion;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Eduardo
 */
public class C_RelCarrera extends HttpServlet {
   
    String query = "";
    String[] carrera;
    String[] caballo;
    String[] peso;
    String[] posicion;
    
    int ID_Carrera = 0;
    int ID_Caballo = 0;
    int pesoI = 0;
    int posicionI = 0;

    JConexion jc = new JConexion("");
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, InstantiationException, IllegalAccessException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            this.carrera = request.getParameterValues("ID_Carrera");
            this.caballo = request.getParameterValues("ID_Caballo");
            this.peso = request.getParameterValues("peso");
            this.posicion = request.getParameterValues("posicion");

            for(int i=0; i<1; i++){
                //System.out.println("ID_Caballo:" + this.caballo[i].toString().length());
                //System.out.println("ID_Carrera:" + this.carrera[i].toString().length());
                //System.out.println("Peso:" + this.peso[i].toString().length());
                //System.out.println("Posicion:" + this.posicion[i].toString().length());
                this.ID_Caballo = Integer.parseInt(this.caballo[i].toString().substring(0,this.caballo[i].toString().length()-2));
                this.ID_Carrera = Integer.parseInt(this.carrera[i].toString().substring(0,this.carrera[i].toString().length()-2));
                this.pesoI = Integer.parseInt(this.peso[i].toString());
                this.posicionI = Integer.parseInt(this.posicion[i].toString());
            }
            for(int i=0; i<1; i++){            
                System.out.println("ID_Caballo:" + this.ID_Caballo);
                System.out.println("ID_Carrera:" + this.ID_Carrera);
                System.out.println("Peso:" + this.pesoI);
                System.out.println("Posicion:" + this.posicionI);
            }
            this.query = "Insert Into royal_mowes.rel_carrera_caballo(FK_ID_Carrera,FK_ID_Caballo, Posicion_Salida, Peso) Values(" + this.ID_Carrera + ", " + this.ID_Caballo + ", " + this.posicionI + "," + this.pesoI + ")";
            System.out.println(this.query);

            jc.MySQLArray("MySQLRM", "Insert", this.query);

            out.println("<html><head><title>Redireccionado</title></head>"
                        + "<body><center><br><br><big><big><font face=\"verdana\">Carrera Registrada</font></big></big><br><br><br><br>"
                        + "<a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/caballo_carrera.jsp\">Regresar</a>"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/login.jsp\">Paginal Principal</a></center></body></html>");

            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet C_RelCarrera</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet C_RelCarrera at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_RelCarrera.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
